package com.finace.money.ui.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.content.LocalBroadcastManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.finace.money.R;
import com.finace.money.common.Constant;
import com.finace.money.common.util.ACache;
import com.finace.money.common.util.ParamJsonUtil;
import com.finace.money.common.util.PopuUtil;
import com.finace.money.common.util.StringUtil;
import com.finace.money.common.util.TabLayoutUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.QryData;
import com.finace.money.data.bean.StatusListBean;
import com.finace.money.data.bean.TickBean;
import com.finace.money.di.component.AppComponent;
import com.finace.money.net.socket.ConnManager;
import com.finace.money.net.socket.PushReceiver;
import com.finace.money.presenter.MainHavePresenter;
import com.finace.money.ui.activity.MainActivity;
import com.finace.money.ui.adapter.HaveListViewAdapter;
import com.finace.money.ui.widget.CustomHScrollView;
import com.finace.money.ui.widget.MyPopupWindow;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import butterknife.BindView;

/**
 * Created by zhangpeng on 2017/11/23.
 */

public class MainHaveFragment extends BaseFragment implements View.OnClickListener {

    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;
    @BindView(R.id.tv_head_title)
    TextView tvHeadTitle;
    @BindView(R.id.button_spinner)
    Button buttonSpinner;
    @BindView(R.id.iv_count_minus)
    ImageView ivCountMinus;
    @BindView(R.id.iv_count_plus)
    ImageView ivCountPlus;
    @BindView(R.id.iv_price_minus)
    ImageView ivPriceMinus;
    @BindView(R.id.iv_price_plus)
    ImageView ivPricePlus;

    /*@BindView(R.id.button_buy)
    Button buttonBuy;
    @BindView(R.id.button_sale)
    Button buttonSale;
    @BindView(R.id.button_clear)
    Button buttonClear;*/


    @BindView(R.id.tab_have)
    TabLayout tabHaveStatus;
    @BindView(R.id.h_scrollView)
    CustomHScrollView mScrollView;
    @BindView(R.id.list_view)
    ListView mListView;
    @BindView(R.id.content_main)
    RelativeLayout mHead;
    @BindView(R.id.textView_1)
    TextView textView1;
    @BindView(R.id.textView_2)
    TextView textView2;
    @BindView(R.id.textView_3)
    TextView textView3;
    @BindView(R.id.textView_4)
    TextView textView4;
    @BindView(R.id.textView_5)
    TextView textView5;
    @BindView(R.id.textView_6)
    TextView textView6;
    @BindView(R.id.textView_7)
    TextView textView7;
    @BindView(R.id.textView_8)
    TextView textView8;
    @BindView(R.id.textView_9)
    TextView textView9;
    @BindView(R.id.tv_trade_price)
    TextView tvTradePrice;
    @BindView(R.id.tv_trade_size)
    TextView tvTradeSize;
    @BindView(R.id.tv_ask_price)
    TextView tvAskPrice;
    @BindView(R.id.tv_ask_size)
    TextView tvAskSize;
    @BindView(R.id.tv_bid_price)
    TextView tvBidPrice;
    @BindView(R.id.tv_bid_size)
    TextView tvBidSize;
    @BindView(R.id.et_count)
    EditText etCount;
    @BindView(R.id.et_price)
    EditText etPrice;


    @BindView(R.id.tv_buy_price)
    TextView tvBuyPrice;
    @BindView(R.id.tv_buy_tip)
    TextView tvBuyTip;
    @BindView(R.id.ll_buy)
    LinearLayout llBuy;

    @BindView(R.id.tv_sale_price)
    TextView tvSalePrice;
    @BindView(R.id.tv_sale_tip)
    TextView tvSaleTip;
    @BindView(R.id.ll_sale)
    LinearLayout llSale;

    @BindView(R.id.tv_clear_count)
    TextView tvClearCount;
    @BindView(R.id.tv_clear_tip)
    TextView tvClearTip;
    @BindView(R.id.ll_clear)
    LinearLayout llClear;

    private List<QryData> mDataList = new ArrayList<>();
    private HaveListViewAdapter mAdapter;
    private int leftPos;//用于记录CustomHScrollView的初始位置
    private int topPos;
    private int tabPosition;
    private final int TAB_HAVE = 0;//持仓
    private final int TAB_KILL = 1;//可撤
    private final int TAB_PLEASE = 2;//委托
    private final int TAB_DONE = 3;//成交
    private MyPopupWindow creatPopupWindow;
    private MyPopupWindow killPopupWindow;
    private OptionsPickerView pvOptions;
    private int position = 0;
    private ArrayList<String> mList;
    private List<StatusListBean> mStatusLists;
    private String m_productID;
    private String m_ExchangeID;
    private double m_tickSize = -1;
    private int mRangeCount = 1;
    private TickBean mTickBean;
    private Button buttonCreatCommitSure;
    private String mAccount;
    private ACache mACache;
    private MainHavePresenter mainHavePresenter = new MainHavePresenter();
    private String mOrderId;
    private boolean mStatusFlag;
    private int maxCount = -1;
    private boolean isClear = false;
    private boolean changePriceFlag = true;//实时价格变动
    private ArrayList<QryData> mHaves = new ArrayList<>();


    @Override
    public int setLayout() {
        return R.layout.fragment_have;
    }

    @Override
    public void init() {

        initView();
        initACache();
        initList();
        clickItem();
        showChooseDeal();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(PushReceiver.ACTION_TICK);
        intentFilter.addAction(PushReceiver.ACTION_QRY);
        intentFilter.addAction(PushReceiver.ACTION_QRY_ERROR);
        intentFilter.addAction(PushReceiver.ACTION_LOGIN);
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(broadcastReceiver, intentFilter);
        unregisterReceiver(broadcastReceiver);
    }

    private void initACache() {

        mACache = ACache.get(UIUtil.getContext());
        mStatusLists = (List<StatusListBean>) mACache.getAsObject(PushReceiver.ACTION_LIST);
        mAccount = mACache.getAsString(Constant.account);
        mList = new ArrayList<>();
        if (null != mStatusLists && mStatusLists.size() > 0) {
            if (null != mStatusLists.get(position) && null != mStatusLists.get(position).getM_Body().getTickbean()) {
                setData(mStatusLists.get(position).getM_Body().getTickbean());
            }
            for (StatusListBean bean : mStatusLists) {
                mList.add(bean.getM_Body().getM_ProductName());
            }
            buttonSpinner.setText(mList.get(position));
            m_productID = mStatusLists.get(position).getM_Body().getM_ProductID();
            m_ExchangeID = mStatusLists.get(position).getM_Body().getM_ExchangeID();
            m_tickSize = mStatusLists.get(position).getM_Body().getM_TickSize();

        }
        initOptionPicker();
        initData();
    }

    private void initView() {

        tvHeadTitle.setText(R.string.deal);
        tvBuyTip.setText(R.string.buy_duo);
        tvSaleTip.setText(R.string.sale_kong);
        tabHaveStatus.addTab(tabHaveStatus.newTab().setText(R.string.have_have));
        tabHaveStatus.addTab(tabHaveStatus.newTab().setText(R.string.have_kill));
        tabHaveStatus.addTab(tabHaveStatus.newTab().setText(R.string.have_please));
        tabHaveStatus.addTab(tabHaveStatus.newTab().setText(R.string.have_deal));
        TabLayoutUtil.reflex(tabHaveStatus);
        rlLeft.setVisibility(View.GONE);
        llBuy.setOnClickListener(this);
        llSale.setOnClickListener(this);
        llClear.setOnClickListener(this);
        buttonSpinner.setOnClickListener(this);
        ivCountMinus.setOnClickListener(this);
        ivCountPlus.setOnClickListener(this);
        ivPriceMinus.setOnClickListener(this);
        ivPricePlus.setOnClickListener(this);
        tabHaveStatus.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tabPosition = tab.getPosition();
                initList();//刷新，重新加载数据
                mScrollView.smoothScrollTo(leftPos, topPos);//每次刷新数据都让CustomHScrollView回到初始位置，避免错乱
                initData();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        etPrice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                changePriceFlag = false;
                tvBuyPrice.setText(charSequence.toString());
                tvSalePrice.setText(charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            TickBean tick = (TickBean) intent.getSerializableExtra(PushReceiver.DATA);
            String qryPosition = intent.getStringExtra(PushReceiver.QRY_POSITION_END);
            String qryOrder = intent.getStringExtra(PushReceiver.QRY_ORDER_END);
            String qryTrade = intent.getStringExtra(PushReceiver.QRY_TRADE_END);
            String errorMsg = intent.getStringExtra(PushReceiver.ACTION_QRY_ERROR);
            if (null != tick && m_productID.equals(tick.getSymbol())) {
                mTickBean = tick;
                setData(mTickBean);
            }
            if (StringUtil.isNotNullString(qryPosition)) {
                initData();
            }
            if (StringUtil.isNotNullString(qryOrder)) {
                initData();
            }
            if (StringUtil.isNotNullString(qryTrade)) {
                initData();
            }
            if (StringUtil.isNotNullString(errorMsg)) {
                UIUtil.showToastSafe(errorMsg);
            }
        }
    };

    private void setData(TickBean tick) {
        if (null != tick) {
            tvTradePrice.setText(tick.getTrade().trim());
            tvTradeSize.setText(tick.getSize().trim());
            tvAskPrice.setText(tick.getAsk().trim());
            tvAskSize.setText(tick.getAsksize().trim());
            tvBidPrice.setText(tick.getBid().trim());
            tvBidSize.setText(tick.getBidsize().trim());
            if(changePriceFlag){
                tvBuyPrice.setText(tick.getAsk());
                tvSalePrice.setText(tick.getBid());
            }
        } else {
            if(changePriceFlag){
                tvBuyPrice.setText("-");
                tvSalePrice.setText("-");
                etCount.setText("");
                etPrice.setText("");
            }
            tvTradePrice.setText("-");
            tvTradeSize.setText("-");
            tvAskPrice.setText("-");
            tvAskSize.setText("-");
            tvBidPrice.setText("-");
            tvBidSize.setText("-");

        }

    }

    private void clickItem() {

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (tabPosition) {
                    case TAB_HAVE:
                        isClear = true;
                        changePriceFlag = false;
                        changeShow(mDataList.get(i));
                        llClear.setBackgroundResource(R.drawable.back_status_button_white);
                        llClear.setEnabled(true);
                        break;
                    case TAB_KILL:
                        killOrder(mDataList.get(i));
                        mOrderId = mDataList.get(i).getOrderId();
                        break;
                    case TAB_PLEASE:
                        break;
                    case TAB_DONE:
                        break;
                }
            }
        });
    }

    /**
     * 持仓选中时更改页面按钮状态
     *
     * @param qryData
     */
    private void changeShow(QryData qryData) {

        if (null != qryData) {
            maxCount = Integer.valueOf(qryData.getText4());
            mStatusFlag = qryData.getText2().equals(getString(R.string.duo));
            for (int i = 0; i < mStatusLists.size(); i++) {
                if (qryData.getText1().equals(mStatusLists.get(i).getM_Body().getM_ProductID())) {
                    position = i;
                    break;
                }
            }
            StatusListBean snapBean = mStatusLists.get(position);
            m_productID = snapBean.getM_Body().getM_ProductID();
            m_ExchangeID = snapBean.getM_Body().getM_ExchangeID();
            m_tickSize = snapBean.getM_Body().getM_TickSize();
            buttonSpinner.setText(mList.get(position));

            if (maxCount != -1 && isClear) {
                tvClearCount.setText(qryData.getText3());
            } else {
                tvClearCount.setText(UIUtil.getString(R.string.have_null));
            }
            etCount.setText(qryData.getText4());
            etPrice.setText(qryData.getText3());

            mHaves = mainHavePresenter.getHaveData(m_productID,mHaves,mTickBean);
            changeShowStatus(mStatusFlag, qryData.getText3(),mHaves.size());

            TickBean snapTick = snapBean.getM_Body().getTickbean();
            if(null != snapTick){
                tvTradePrice.setText(snapTick.getTrade().trim());
                tvTradeSize.setText(snapTick.getSize().trim());
                tvAskPrice.setText(snapTick.getAsk().trim());
                tvAskSize.setText(snapTick.getAsksize().trim());
                tvBidPrice.setText(snapTick.getBid().trim());
                tvBidSize.setText(snapTick.getBidsize().trim());
            }

        }
    }

    /**
     * 更改按钮状态
     *  @param flag true 为多
     * @param text 价格
     * @param size 是否多空都有
     */
    private void changeShowStatus(boolean flag, String text, int size) {
        tvBuyPrice.setText(text);
        tvSalePrice.setText(text);
        if(size == 2){
            tvBuyTip.setText(R.string.jia_duo);
            tvSaleTip.setText(R.string.jia_kong);
        }else {
            if (flag) {
                tvBuyTip.setText(R.string.jia_duo);
                tvSaleTip.setText(R.string.suo_cang);
            } else {
                tvBuyTip.setText(R.string.suo_cang);
                tvSaleTip.setText(R.string.jia_kong);
            }
        }

    }

    /**
     * 委托撤单
     *
     * @param qryData
     */
    private void killOrder(QryData qryData) {
        killPopupWindow = new PopuUtil().initAtLocationPopuWrap(getActivity(),
                R.layout.popup_main_kill, llContent, 303, 300);
        TextView account = killPopupWindow.getContentView().findViewById(R.id.tv_kill_account);
        TextView symbol = killPopupWindow.getContentView().findViewById(R.id.tv_kill_symbol);
        TextView symbolCode = killPopupWindow.getContentView().findViewById(R.id.tv_kill_symbol_code);
        TextView count = killPopupWindow.getContentView().findViewById(R.id.tv_kill_count);
        TextView price = killPopupWindow.getContentView().findViewById(R.id.tv_kill_price);
        Button buttonKillOrder = killPopupWindow.getContentView().findViewById(R.id.button_kill_order);
        Button buttonKillCancel = killPopupWindow.getContentView().findViewById(R.id.button_kill_cancel);
        buttonKillOrder.setOnClickListener(this);
        buttonKillCancel.setOnClickListener(this);
        String name = null;
        for (StatusListBean bean : mStatusLists) {
            if (bean.getM_Body().getM_ProductID().equals(qryData.getText1())) {
                name = bean.getM_Body().getM_ProductName();
                break;
            }
        }
        account.setText(mAccount);
        symbol.setText(name);
        symbolCode.setText(qryData.getText1());
        count.setText(qryData.getText5());
        price.setText(qryData.getText4());

    }

    /**
     * 委托买入
     */
    private void creatOrder(String buttonContent) {
        creatPopupWindow = new PopuUtil().initAtLocationPopuWrap(getActivity(),
                R.layout.popup_main_creat, llContent, 284, 290);

        TextView tv_account_name = creatPopupWindow.getContentView().findViewById(R.id.tv_account_name);
        TextView tv_symbol_name = creatPopupWindow.getContentView().findViewById(R.id.tv_symbol_name);
        TextView tv_code_name = creatPopupWindow.getContentView().findViewById(R.id.tv_code_name);
        TextView tv_please_count = creatPopupWindow.getContentView().findViewById(R.id.tv_please_count);
        TextView tv_please_price = creatPopupWindow.getContentView().findViewById(R.id.tv_please_price);
        Button buttonCreatCancel = creatPopupWindow.getContentView().findViewById(R.id.button_creat_cancel);
        buttonCreatCommitSure = creatPopupWindow.getContentView().findViewById(R.id.button_creat_commit_sure);
        buttonCreatCommitSure.setText(buttonContent);

        StatusListBean bean = mStatusLists.get(position);
        tv_account_name.setText(mAccount);
        tv_symbol_name.setText(bean.getM_Body().getM_ProductName());
        tv_code_name.setText(bean.getM_Body().getM_ProductID());
        tv_please_count.setText(etCount.getText().toString());
        tv_please_price.setText(etPrice.getText().toString());
        CheckBox checkboxTip = creatPopupWindow.getContentView().findViewById(R.id.checkbox_tip);
        checkboxTip.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

            }
        });
        buttonCreatCancel.setOnClickListener(this);
        buttonCreatCommitSure.setOnClickListener(this);
    }

    private void initList() {

        switch (tabPosition) {
            case TAB_HAVE://持仓
                textView1.setText(R.string.contract_name);
                textView2.setText(R.string.duo_kong);
                textView3.setText(R.string.open_price_ava);
                textView4.setText(R.string.total_count);
                textView5.setText(R.string.available);
                textView6.setText(R.string.p_l);
                textView7.setVisibility(View.GONE);
                textView8.setVisibility(View.GONE);
                textView9.setVisibility(View.GONE);
                break;
            case TAB_KILL://可撤
                textView1.setText(R.string.contract_name);
                textView2.setText(R.string.business);
                textView3.setText(R.string.kai_ping);
                textView4.setText(R.string.price_gua_dan);
                textView5.setText(R.string.surplus_count_gua_dan);
                textView6.setVisibility(View.GONE);
                textView7.setVisibility(View.GONE);
                textView8.setVisibility(View.GONE);
                textView9.setVisibility(View.GONE);
                break;
            case TAB_PLEASE://委托
                textView1.setText(R.string.contract_name);
                textView2.setText(R.string.status);
                textView3.setText(R.string.business);
                textView4.setText(R.string.kai_ping);
                textView5.setText(R.string.please_price);
                textView6.setText(R.string.please_count);
                textView7.setText(R.string.have_deal);
                textView8.setText(R.string.please_time);
                textView6.setVisibility(View.VISIBLE);
                textView7.setVisibility(View.VISIBLE);
                textView8.setVisibility(View.VISIBLE);
                textView9.setVisibility(View.GONE);
                break;
            case TAB_DONE://成交
                textView1.setText(R.string.contract_name);
                textView2.setText(R.string.business);
                textView3.setText(R.string.kai_ping);
                textView4.setText(R.string.deal_price_have);
                textView5.setText(R.string.deal_count_have);
                textView6.setText(R.string.deal_time_have);
                textView7.setVisibility(View.GONE);
                textView8.setVisibility(View.GONE);
                textView9.setVisibility(View.GONE);
                break;
        }
        mHead.setFocusable(true);
        mHead.setClickable(true);
        mHead.setOnTouchListener(new MyTouchLinstener());
        mListView.setOnTouchListener(new MyTouchLinstener());
        initData();
    }


    class MyTouchLinstener implements View.OnTouchListener {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            //当在表头和listView控件上touch时，将事件分发给 ScrollView
            HorizontalScrollView headSrcrollView = (HorizontalScrollView) mHead.findViewById(R.id.h_scrollView);
            headSrcrollView.onTouchEvent(arg1);
            return false;
        }
    }

    private void initData() {

        mDataList.clear();
        switch (tabPosition) {
            case TAB_HAVE:
                List<QryData> positionData = mainHavePresenter.getPositionData(mTickBean);
                mDataList.addAll(positionData);
                break;
            case TAB_KILL:
                List<QryData> orderKillData = mainHavePresenter.getOrderKillData();
                mDataList.addAll(orderKillData);
                break;
            case TAB_PLEASE:
                List<QryData> orderPleaseData = mainHavePresenter.getOrderPleaseData();
                mDataList.addAll(orderPleaseData);
                break;
            case TAB_DONE:
                List<QryData> tradeData = mainHavePresenter.getTradeData();
                mDataList.addAll(tradeData);
                break;
        }
        if (null != mAdapter) {
            mAdapter.notifyDataSetChanged();
        } else {
            mAdapter = new HaveListViewAdapter(mHead);
            mListView.setAdapter(mAdapter);
            mAdapter.setmData(mDataList);
        }

    }

    /**
     * 记录CustomHScrollView的初始位置
     *
     * @param l
     * @param t
     */
    public void setPosData(int l, int t) {
        this.leftPos = l;
        this.topPos = t;
    }

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {

    }

    @Override
    public void onClick(View view) {
        String currentPrice = etPrice.getText().toString();
        String currentCount = etCount.getText().toString();
        switch (view.getId()) {
            case R.id.rl_left:
                break;
            case R.id.button_spinner:
                chooseSymbols();
                break;
            case R.id.iv_count_minus:
                changePriceFlag = false;
                if (StringUtil.isNotNullString(currentCount)) {
                    int newCount = Integer.valueOf(currentCount) - mRangeCount;
                    if (newCount <= 0) {
                        newCount = 1;
                    }
                    etCount.setText(String.valueOf(newCount));
                } else {
                    etCount.setText("1");
                }
                break;
            case R.id.iv_count_plus:
                changePriceFlag = false;
                if (StringUtil.isNotNullString(currentCount)) {
                    int newCount = Integer.valueOf(currentCount) + mRangeCount;
                    if (maxCount != -1 && newCount >= maxCount) {
                        newCount = maxCount;
                    }
                    etCount.setText(String.valueOf(newCount));
                } else {
                    etCount.setText("1");
                }
                break;
            case R.id.iv_price_minus:
                changePriceFlag = false;
                if (m_tickSize == -1) {
                    return;
                }
                if (StringUtil.isNotNullString(currentPrice)) {
                    double newPrice = Double.valueOf(currentPrice) - m_tickSize;
                    if (newPrice <= m_tickSize) {
                        newPrice = m_tickSize;
                    }
                    etPrice.setText(String.valueOf(newPrice));
                    tvBuyPrice.setText(String.valueOf(newPrice));
                    tvSalePrice.setText(String.valueOf(newPrice));
                    tvClearCount.setText(String.valueOf(newPrice));
                } else {
                    etPrice.setText(String.valueOf(m_tickSize));
                    tvBuyPrice.setText(String.valueOf(m_tickSize));
                    tvSalePrice.setText(String.valueOf(m_tickSize));
                    tvClearCount.setText(String.valueOf(m_tickSize));
                }
                break;
            case R.id.iv_price_plus:
                changePriceFlag = false;
                if (m_tickSize == -1) {
                    return;
                }
                if (StringUtil.isNotNullString(currentPrice)) {
                    double newPrice = Double.valueOf(currentPrice) + m_tickSize;
                    etPrice.setText(String.valueOf(newPrice));
                    tvBuyPrice.setText(String.valueOf(newPrice));
                    tvSalePrice.setText(String.valueOf(newPrice));
                } else {
                    etPrice.setText(String.valueOf(m_tickSize));
                    tvBuyPrice.setText(String.valueOf(m_tickSize));
                    tvSalePrice.setText(String.valueOf(m_tickSize));
                }
                break;
            //委托撤单弹窗
            case R.id.button_kill_order:
                TreeMap<String, String> killDetailMap = new TreeMap<>();
                killDetailMap.put("order_id", mOrderId);
                TreeMap<String, String> killDetail = new TreeMap<>();
                killDetail.put("msgtype", "CancelOrder");
                killDetail.put("payload", ParamJsonUtil.getjsonData(killDetailMap).toString());
                ConnManager.getInstance().sendMessage(ParamJsonUtil.getjsonData(killDetail).toString());
                killPopupWindow.dismiss();
                break;
            case R.id.button_kill_cancel:
                killPopupWindow.dismiss();
                break;
            //买入弹窗点击
            case R.id.button_creat_cancel:
                creatPopupWindow.dismiss();
                break;
            case R.id.button_creat_commit_sure:
                String side;
                String offset_flag;
                int toadySize;
                if (UIUtil.getString(R.string.buy_sure).equals(buttonCreatCommitSure.getText().toString())) {
                    side = "true";
                    offset_flag = "OPEN";
                } else if (UIUtil.getString(R.string.sale_sure).equals(buttonCreatCommitSure.getText().toString())) {
                    side = "false";
                    offset_flag = "OPEN";
                } else if (mStatusFlag) {
                    side = "false";
                    offset_flag = "CLOSE";
                } else {
                    side = "true";
                    offset_flag = "CLOSE";
                }
                if (m_ExchangeID.equals("SHFE") && UIUtil.getString(R.string.clear_sure).equals(buttonCreatCommitSure.getText().toString())) {//上期所例外){
                    toadySize = mainHavePresenter.getTodaySize(side,mHaves);
                    int yesSize = maxCount - toadySize;
                    if(yesSize == 0){
                        doDeal(side, "CLOSETODAY",currentCount);
                    }else if(Integer.valueOf(currentCount) > yesSize){
                        doDeal(side, "CLOSEYESTERDAY",String.valueOf(yesSize));
                        doDeal(side, "CLOSETODAY",String.valueOf(Integer.valueOf(currentCount)-yesSize));
                    }else {
                        doDeal(side, "CLOSEYESTERDAY",currentCount);
                    }
                }else {
                    doDeal(side, offset_flag,currentCount);
                }

                creatPopupWindow.dismiss();
                break;
            case R.id.ll_buy:
                if (StringUtil.isNullString(currentPrice)) {
                    UIUtil.showToastSafe(getString(R.string.input_price_please));
                    return;
                }
                if (StringUtil.isNullString(currentCount)) {
                    UIUtil.showToastSafe(getString(R.string.count_input_please));
                    return;
                }
                if (Integer.valueOf(currentCount) <= 0) {
                    UIUtil.showToastSafe(getString(R.string.choose_one_please));
                    return;
                }
                creatOrder(UIUtil.getString(R.string.buy_sure));
                break;
            case R.id.ll_sale:
                creatOrder(UIUtil.getString(R.string.sale_sure));
                break;
            case R.id.ll_clear:
                if (isClear) {
                    creatOrder(UIUtil.getString(R.string.clear_sure));
                } else {
                    UIUtil.showToastSafe(getString(R.string.choose_one_order_please));
                }
                break;
        }
    }

    private void doDeal(String side, String offset_flag, String size) {
        TreeMap<String, String> twoDetailMap = new TreeMap<>();
        twoDetailMap.put("account", mAccount);
        twoDetailMap.put("symbol", m_productID);
        twoDetailMap.put("size", size);
        twoDetailMap.put("price", etPrice.getText().toString());
        twoDetailMap.put("side", side);
        twoDetailMap.put("offset_flag", offset_flag);
        TreeMap<String, String> mapDetail = new TreeMap<>();
        mapDetail.put("msgtype", "PlaceOrderRequest");
        mapDetail.put("payload", ParamJsonUtil.getjsonData(twoDetailMap).toString());
        ConnManager.getInstance().sendMessage(ParamJsonUtil.getjsonData(mapDetail).toString());
    }

    private void chooseSymbols() {

        pvOptions.setSelectOptions(position);
        pvOptions.show();

    }

    private void initOptionPicker() {
        if (mList.size() > 0) {
            pvOptions = new OptionsPickerView.Builder(getActivity(), new OptionsPickerView.OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int options2, int options3, View v) {
                    isClear = false;
                    changePriceFlag = true;
                    position = options1;
                    showSelect();

                }
            })
                    .setTitleBgColor(Color.BLACK)
                    .setCancelColor(Color.WHITE)
                    .setSubmitColor(Color.WHITE)
                    .setContentTextSize(14)//设置滚轮文字大小
                    .setDividerColor(Color.LTGRAY)//设置分割线的颜色
                    .setLineSpacingMultiplier(12f)
                    .setSelectOptions(0)//默认选中项
                    .setBgColor(Color.BLACK)
                    .setCancelColor(Color.GRAY)
                    .setSubmitColor(Color.GRAY)
                    .setTextColorOut(Color.WHITE)
                    .setTextColorCenter(Color.WHITE)
                    .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                    .setBackgroundId(0x66000000) //设置外部遮罩颜色
                    .build();

            pvOptions.setPicker(mList);//一级选择器
        }

    }

    private void showSelect() {
        buttonSpinner.setText(mList.get(position));
        m_productID = mStatusLists.get(position).getM_Body().getM_ProductID();
        m_tickSize = mStatusLists.get(position).getM_Body().getM_TickSize();
        m_ExchangeID = mStatusLists.get(position).getM_Body().getM_ExchangeID();

        mHaves = mainHavePresenter.getHaveData(m_productID,mHaves,mTickBean);

        /*List<QryData> positionData = mainHavePresenter.getPositionData(mTickBean);
        mHaves.clear();
        for (int i = 0; i < positionData.size(); i++) {
            if (positionData.get(i).getText1().equals(m_productID)) {
                mHaves.add(positionData.get(i));
            }
        }*/
        if (null != mStatusLists.get(position).getM_Body().getTickbean()) {
            setData(mStatusLists.get(position).getM_Body().getTickbean());
        } else {
            setData(null);
        }
        if (mHaves.size() == 2) {
            tvBuyPrice.setText(mHaves.get(0).getText3());
            tvSalePrice.setText(mHaves.get(0).getText3() );
            tvClearCount.setText(mHaves.get(0).getText3() );
            tvBuyTip.setText(R.string.jia_duo);
            tvSaleTip.setText(R.string.jia_kong);
            etPrice.setText(mHaves.get(0).getText3());
            maxCount = -1;
        } else if (mHaves.size() == 1) {
            changeShow(mHaves.get(0));
            maxCount = Integer.valueOf(mHaves.get(0).getText4());
        } else {
            maxCount = -1;
            etCount.setText("1");
            tvBuyTip.setText(R.string.buy_duo);
            tvSaleTip.setText(R.string.sale_kong);
        }
        llClear.setBackgroundResource(R.drawable.back_status_button_gray);
        llClear.setEnabled(false);

    }

    public void refreash() {
        if (null != mAdapter) {
            mAdapter.notifyDataSetChanged();
            showChooseDeal();
        }
    }

    private void showChooseDeal() {
        String id = ((MainActivity) getActivity()).getmTickId();
        position = mainHavePresenter.getDealPosition(id, mStatusLists);
        showSelect();
    }
}
